using System;
using Common;

namespace ObjectPool
{
    public abstract class ObjectBase : IReference
    {
        private string m_Name;
        private object m_Target;
        private bool m_Locked;
        private int m_Priority;
        private DateTime m_LastUseTime;

        /// <summary>
        /// 初始化对象基类的新实例
        /// </summary>
        public ObjectBase()
        {
            m_Name = null;
            m_Target = null;
            m_Locked = false;
            m_Priority = 0;
            m_LastUseTime = DateTime.Now;
        }

        /// <summary>
        /// 获取对象名称
        /// </summary>
        public string Name
        {
            get { return m_Name; }
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        public object Target
        {
            get { return m_Target; }
        }


        /// <summary>
        /// 获取或设置对象是否被加锁
        /// </summary>
        public bool Locked
        {
            get { return m_Locked; }
            set { m_Locked = value; }
        }

        /// <summary>
        /// 获取或设置对象的优先级s
        /// </summary>
        public int Priority
        {
            get { return m_Priority; }
            set { m_Priority = value; }
        }

        /// <summary>
        /// 获取自定义释放检查标记
        /// </summary>
        public virtual bool CustomCanReleaseFlag
        {
            get { return true; }
        }

        /// <summary>
        /// 获取对象上次使用时间
        /// </summary>
        public DateTime LastUseTime
        {
            get { return m_LastUseTime; }
            internal set { m_LastUseTime = value; }
        }

        /// <summary>
        /// 初始化对象基类
        /// </summary>
        /// <param name="target"></param>
        protected void Initialize(object target)
        {
            Initialize(null, target, false, 0);
        }

        /// <summary>
        /// 初始化对象基类
        /// </summary>
        /// <param name="name"></param>
        /// <param name="target"></param>
        protected void Initialize(string name, object target)
        {
            Initialize(name, target, false, 0);
        }

        /// <summary>
        /// 初始化对象基类
        /// </summary>
        /// <param name="name"></param>
        /// <param name="target"></param>
        /// <param name="locked"></param>
        protected void Initialize(string name, object target, bool locked)
        {
            Initialize(name, target, locked, 0);
        }
        
        /// <summary>
        /// 初始化对象基类
        /// </summary>
        /// <param name="name"></param>
        /// <param name="target"></param>
        /// <param name="locked"></param>
        /// <param name="priority"></param>
        /// <exception cref="Exception"></exception>
        protected void Initialize(string name, object target, bool locked, int priority)
        {
            if (target == null)
                throw new Exception(string.Format("Target '{0}' is invalid."));
            m_Name = name ?? string.Empty;
            m_Target = target;
            m_Locked = locked;
            m_Priority = priority;
            m_LastUseTime = DateTime.Now;
        }

        /// <summary>
        /// 清理对象基类
        /// </summary>
        public virtual void Clear()
        {
            m_Name = null;
            m_Target = null;
            m_Locked = false;
            m_Priority = 0;
            m_LastUseTime = DateTime.Now;
        }

        /// <summary>
        /// 获取对象时的事件
        /// </summary>
        protected internal virtual void OnSpawn()
        {
        }
        
        /// <summary>
        /// 回收对象时的事件
        /// </summary>
        protected internal virtual void OnUnSpawn()
        {
        }
        
        /// <summary>
        /// 释放对象
        /// </summary>
        /// <param name="isShutdown"></param>
        protected internal abstract void Release(bool isShutdown);

    }
}